<?php
defined('IN_MAIN') or exit('Invalid Access!');

function delete_folder_file($path){
/*
This function will delete all the files and sub-folders and the input folder path itself, if no error found, it will return true.
if input path does not exist, it will return false. If it encounters error while removing files / folder, it will return that particular
file / folder
*/

if(!file_exists($path)){
 return false;
}

if(is_dir($path)){
 //get all files and sub-directory of path
 $dir_run_index=0;
 $dir[0]["dir"]=$path;
 $dir_index=1;
 do{
  if ($dh = opendir($dir[$dir_run_index]["dir"])) {
   while (($file = readdir($dh)) !== false) {
    if($file!="." && $file!=".."){
     $current_file_path=$dir[$dir_run_index]["dir"]."/".$file;
     if(is_dir($current_file_path)){
      $dir[$dir_index]["dir"]=$current_file_path;
      $dir_index++;
     }else{
      $dir[$dir_run_index]["file"][]=$current_file_path;
     }
    }
   }
   closedir($dh);
  }
  $dir_run_index++;
 }while(isset($dir[$dir_run_index]["dir"]));

 //delete all files and sub-directory and path itself
 for($i=count($dir)-1;$i>=0;$i--){
  if($t=count($dir[$i]["file"])){
   for($j=0;$j<$t;$j++){
    if(!@unlink($dir[$i]["file"][$j])){
     $fail_files.=$dir[$i]["file"][$j]."<br />\n";
    }
   }
  }
  if(!@rmdir($dir[$i]["dir"])){
   $fail_files.=$dir[$i]["dir"]."<br />\n";
  }
 }
}elseif(!@unlink($path)){
 $fail_files.=$path;
}

if($fail_files){
 $return=$fail_files;
}else{
 $return=true;
}

return $return;
}
?>